package net.fxug.hokuriku.chocolat.service
{
    import flash.events.TimerEvent;
    import flash.utils.Timer;

    import net.fxug.hokuriku.chocolat.events.VoteEvent;
    import net.fxug.hokuriku.chocolat.events.VoteStatusEvent;
    import net.fxug.hokuriku.chocolat.model.VoteData;

    /**
     * IVoteWatcherの実装のMockクラス
     * デバッグしやすいように10秒に1回実行Math.random()でつぶやきを返し続ける。
     * @author hikaruworld
     */
    public class VoteWatcherMock implements IVoteWatcher
    {
        private var timer:Timer;

        public function VoteWatcherMock()
        {
            //TODO: implement function
        }

        public function getInterfaceName():String
        {
            return "voteWatcherMock";
        }

        public function start():void
        {
            this.timer = new Timer(1000 * 10, 0);
            this.timer.addEventListener(TimerEvent.TIMER, function(e:TimerEvent):void
                {
//                    var vo:VoteManager = VoteManager.getInstance();
//
//                    var tweets:Array = [];
//                    var cnt:int = Math.round(Math.random() * 100);
//                    for (var i:int = 0; i < cnt; i++)
//                    {
//                        var tmp:VoteData = new VoteData();
////                        tmp.id = String(i * timer.currentCount);
//                        tmp.id = String(i);
//                        tmp.text = "Goood!!! #he" + String(i);
//                        tweets.push(tmp);
//                    }
//                    var ve:VoteEvent = new VoteEvent(VoteEvent.GOOD_EVENT, tweets);
//                    vo.dispatchVote(ve);
                    var tweets:Array = [];
                    var tmp:VoteData = new VoteData();
                    tmp.id = "1";
                    tmp.text = "Good!! #good";
                    tweets.push(tmp);
                    var ve:VoteEvent = new VoteEvent(VoteEvent.GOOD_EVENT, tweets);
                    var vo:VoteManager = VoteManager.getInstance();
                    vo.dispatchVote(ve);
                });
            //            timer.start();

            VoteManager.getInstance().addEventListener(VoteStatusEvent.READY_EVENT, initHandler);
            VoteManager.getInstance().onReady(this);
        }

        public function initHandler(event:VoteStatusEvent):void
        {
            VoteManager.getInstance().removeEventListener(VoteStatusEvent.READY_EVENT, initHandler);

            this.timer.start();

        }

        public function destory():void
        {
            if (this.timer != null)
            {
                timer.stop();
            }
        }

        public function isEnabled():Boolean
        {
            if (this.timer == null)
            {
                return false;
            }
            return this.timer.running;
        }
    }
}